﻿using Microsoft.Net.Http.Headers;

namespace Microsoft.AspNetCore.Mvc.Infrastructure;

public class BpctFileStreamResult : FileStreamResult, IBpctFileStreamResult
{
    public string FileInlineName { get; set; } = string.Empty;

    public BpctFileStreamResult(Stream fileStream, string contentType, string? eTag = null)
        : this(fileStream, MediaTypeHeaderValue.Parse(contentType), !string.IsNullOrEmpty(eTag) ? EntityTagHeaderValue.Parse(eTag) : null)
    {

    }

    public BpctFileStreamResult(Stream fileStream, MediaTypeHeaderValue contentType, EntityTagHeaderValue? eTag = null) : base(fileStream, contentType)
    {
        EntityTag = eTag;
        EnableRangeProcessing = true;
    }

    public override Task ExecuteResultAsync(ActionContext context)
    {
        if (context is null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        var executor = context.HttpContext.RequestServices.GetRequiredService<IActionResultExecutor<BpctFileStreamResult>>();

        return executor.ExecuteAsync(context, this);
    }
}